package com.example.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhangs
 * @version v1.0
 * @decription 获取初始化配置参数
 * @date 2021/3/24 17:16
 **/
//@WebServlet(urlPatterns = "/demo06", initParams = {
//        @WebInitParam(name = "username", value = "root", description = "用户名"),
//        @WebInitParam(name = "password", value = "1234", description = "用户密码")
//})
public class Demo06 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取全局配置初始参数
        ServletContext context = getServletContext();
        Object username = context.getInitParameter("username");
        Object password = context.getInitParameter("password");
        resp.getWriter().print("ServletContext 的初始化参数：username:"
                + username + "\npassword:" + password + "\n");
        // 获取单个Servlet的
        String s1 = getServletConfig().getInitParameter("username");
        String s2 = getServletConfig().getInitParameter("password");
        resp.getWriter().print("ServletContext 的初始化参数：username:"
                + s1 + "\npassword:" + s2);
    }
}
